package com.i_art_engineer.meigong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.i_art_engineer.meigong.R;
import com.i_art_engineer.meigong.base.ActionBarActivity;
import com.i_art_engineer.meigong.widget.ActionBar;
import com.i_art_engineer.meigong.widget.LoadingAndRetryManager;
import com.i_art_engineer.meigong.widget.OnLoadingAndRetryListener;

/**
 * 加载网页界面
 */
public class WebViewActivity extends ActionBarActivity {


    public static final String EXTRA_WEB_URL = "web_url";

    private WebView webView;
    private String url;

    LoadingAndRetryManager mLoadingAndRetryManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);

        Intent intent = getIntent();
        url = intent.getStringExtra(EXTRA_WEB_URL);
        initActionBar(R.string.title_webview);

        webView = (WebView) findViewById(R.id.wv_ad_content);
        WebChromeClient wvcc = new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                mLoadingAndRetryManager.showContent();
                Log.d("ANDROID_LAB", "TITLE=" + title);
                actionBar.setTitleView(title);
            }

        };
        // 设置setWebChromeClient对象
        webView.setWebChromeClient(wvcc);

        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });

        LinearLayout mWevViewArea = (LinearLayout) findViewById(R.id.ll_webview_area);

        mLoadingAndRetryManager = LoadingAndRetryManager.generate(mWevViewArea, new OnLoadingAndRetryListener() {
            @Override
            public void setRetryEvent(View retryView) {
                retryRefreashData(retryView);
            }
        });

        refreashData();

    }

    private void refreashData() {
        mLoadingAndRetryManager.showLoading();
        webView.loadUrl(url);
        new Thread() {
            @Override
            public void run() {

                if (Math.random() > 0.6) {
                    mLoadingAndRetryManager.showContent();
                } else {
                    mLoadingAndRetryManager.showRetry();
                }
            }
        }.start();
    }

    public void retryRefreashData(View retryView) {
        View view = retryView.findViewById(R.id.id_btn_retry);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(WebViewActivity.this, "retry event invoked", Toast.LENGTH_SHORT).show();
                WebViewActivity.this.refreashData();
            }
        });
    }

}
